{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Python 01 变量、数据类型与输入输出"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Python是一种弱类型语言，它不强调明晰的数据类型"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "#定义三个变量 分别是整数、小数、字符串\n",
    "age=10\n",
    "salary=5.21\n",
    "name=\"python\""
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型为:\n",
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "#我们可以使用 type(变量名称) 来查看数据类型.\n",
    "print(\"数据类型为:\")\n",
    "print(type(age))\n",
    "print(type(salary))\n",
    "print(type(name))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a= 10\n"
     ]
    }
   ],
   "source": [
    "#给变量赋值非常简单 直接使用=号就行了\n",
    "a=10\n",
    "print(\"a=\",a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1983708725776\n"
     ]
    }
   ],
   "source": [
    "#查看变量a的内存地址\n",
    "print(id(a))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "变量的命名规则： 不能以关键字命名、不能以数字开头"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 赋值 9.0\n",
      "100 100 100\n"
     ]
    }
   ],
   "source": [
    "#python支持多重赋值以及连等操作\n",
    "x,y,z=10,\"赋值\",9.0   #多重赋值\n",
    "print(x,y,z)\n",
    "a=b=c=100   #连等操作\n",
    "print(a,b,c)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Python中赋值的本质是什么？\n",
    "Python中，万物皆对象，Python中基本使用浅复制"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是本函数的注释\n"
     ]
    }
   ],
   "source": [
    "#python中字符串可以由单、双、三引号构成\n",
    "##三引号内可以包含单双引号，双引号内可以包含单引号\n",
    "##引号常用于撰写函数注释 会在必要时显现\n",
    "def function1():\n",
    "    '''这是本函数的注释'''\n",
    "    print(\"函数\")\n",
    "\n",
    "##查看函数注释\n",
    "print(function1.__doc__)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "#使用len来查看字符串的长度\n",
    "a=\"abcde f\"     #空格也占\n",
    "print(len(a))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c=a+b= 你好\n",
      "d=a*3= 你你你\n"
     ]
    }
   ],
   "source": [
    "##python中可以使用+来拼接字符串，使用*来倍增字符串\n",
    "a=\"你\"\n",
    "b=\"好\"\n",
    "c=a+b\n",
    "print(\"c=a+b=\",c)\n",
    "d=a*3\n",
    "print(\"d=a*3=\",d)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 字符串的分割与处理（自行实验）"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    I Like Programming  \n",
      "24\n"
     ]
    }
   ],
   "source": [
    "str1=\"    I Like Programming  \"\n",
    "print(str1)\n",
    "print(len(str1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    I Like Programming\n",
      "22\n"
     ]
    }
   ],
   "source": [
    "#删除末尾空格     这样理解 r意味 right 右边 右边是末尾\n",
    "print(str1.rstrip())\n",
    "print(len(str1.rstrip()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Like Programming  \n",
      "20\n"
     ]
    }
   ],
   "source": [
    "#删除开头空格   l意味 left  左边 左边是开头\n",
    "print(str1.lstrip())\n",
    "print(len(str1.lstrip()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Like Programming\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "#删除首尾空格\n",
    "print(str1.strip())\n",
    "print(len(str1.strip()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    i like programming  \n"
     ]
    }
   ],
   "source": [
    "##将所有字母小写化\n",
    "print(str1.lower())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    I LIKE PROGRAMMING  \n"
     ]
    }
   ],
   "source": [
    "#所有字母大写化\n",
    "print(str1.upper())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    I Like Programming  \n"
     ]
    }
   ],
   "source": [
    "#将每个单词的首字母变为大写\n",
    "print(str1.title())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['I', 'Like', 'Programming']\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "##  分割字符串\n",
    "print(str1.split())     #如果没有指定参数的化，默认按空格划分\n",
    "print(type(str1.split()))   #split方法返回的是一个列表"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'Java', 'C++']\n"
     ]
    }
   ],
   "source": [
    "str2=\"Python|Java|C++\"\n",
    "print(str2.split(\"|\"))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "分割中文请查阅jieba库的文档：https://github.com/fxsjy/jieba"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 输入输出"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "类型错误\n"
     ]
    }
   ],
   "source": [
    "#Python中使用input来接收用户输入\n",
    "a=input(\"Please Input A Number\")\n",
    "print(type(a)) #input的返回类型恒为str\n",
    "if(type(a)!=type(3) and type(a)!=type(3.0)):\n",
    "    print(\"类型错误\")   #为什么输入的是个数字 却仍然提示类型错误呢？\n",
    "    #Python中input函数的返回值类型默认为str\n",
    "else:\n",
    "    print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "\n",
      "Prints the values to a stream, or to sys.stdout by default.\n",
      "Optional keyword arguments:\n",
      "file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "sep:   string inserted between values, default a space.\n",
      "end:   string appended after the last value, default a newline.\n",
      "flush: whether to forcibly flush the stream.\n"
     ]
    }
   ],
   "source": [
    "#输出就直接使用print() 即可\n",
    "print(print.__doc__)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5\n",
      "64\n",
      "1\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#Python中的数学运算和其他语言相近\n",
    "print(3/2)  #C++中默认向下取整，类型不能转换。Python可以更精确\n",
    "print(4**3) #次方运算\n",
    "print(3//2) #整除运算\n",
    "print(3>4)  #这种运算返回的类型为bool"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32mC:\\Users\\ADMINI~1\\AppData\\Local\\Temp/ipykernel_26272/1596790990.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[0ma\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;34m\"1\"\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      4\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtype\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0ma\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 5\u001B[1;33m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0ma\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;36m1\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "#强制类型转换  类型(变量名) int(a)这样子\n",
    "#Python中类型转换要比C++中方便的多\n",
    "a=\"1\"\n",
    "print(type(a))\n",
    "print(a+1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#现在把它转化成int\n",
    "a=int(a)\n",
    "print(type(a))\n",
    "print(a+1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "4\n",
      "3\n",
      "4\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "#浮点数转换为整数   默认采用向下取整原则\n",
    "print(int(4.9))\n",
    "#round()方法采用四舍五入原则\n",
    "print(round(3.7))\n",
    "print(round(3.1))\n",
    "print(round(3.5))   #round方法中的参数为.5时，默认取参数最接近的偶数\n",
    "print(round(4.5))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}